package gui;

import java.util.Arrays;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class EnergyTableModel extends AbstractTableModel implements TableModelListener
{
	private String[] columnNames;
	private Object[][] table;
	
	public EnergyTableModel()
	{
		columnNames = new String[]{"Time", "Produced", "Required"}; //TODO: column names
		table = new Object[][]{{"1:00", new Double(45.6), new Double(90.4)}};
	}

	@Override
	public Class<?> getColumnClass(int columnIndex)
	{
		if(getValueAt(0, columnIndex) != null)
			return getValueAt(0, columnIndex).getClass();
		else
			return String.class;
	}

	@Override
	public int getColumnCount()
	{
		return columnNames.length;
	}

	@Override
	public String getColumnName(int columnIndex)
	{
		return columnNames[columnIndex];
	}

	@Override
	public int getRowCount()
	{
		return table.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		return table[rowIndex][columnIndex];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		return true;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex)
	{
		table[rowIndex][columnIndex] = aValue;
	}

	@Override
	public void tableChanged(TableModelEvent arg0)
	{
		if(arg0.getLastRow() == table.length - 1)
		{
			table = Arrays.copyOf(table, table.length + 1);
		}
		
	}

}
